package com.moon.game.command.guest.player;

import org.apache.log4j.Logger;

import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.constants.Constants;
import com.moon.game.constants.ErrorCode;
import com.moon.game.exception.GException;
import com.moon.game.manager.LevelExpMG;
import com.moon.game.service.PlayerService;
import com.moon.game.vos.ErrorMsg;
import com.moon.game.vos.PlayerVo;

/**
 * @description 升级
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-5-29 下午10:01:50
 */
public class UpgradeLevelCmd extends BaseCmd {
	private static final Logger log = Logger.getLogger(TakeOffEquipCmd.class);
	
	
	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
		case STATU_INIT:
			doInit(req);
			break;
		case STATU_CHECK:
			doCheckSelf(req);
			break;
		case STATU_RETURN:
			doResponse(req);
			break;
		default:
			break;
		}
	}

	@Override
	protected void doResponse(RequestSession req) throws GException {
		PlayerVo player = getPlayerVo(req);
		if (player.getLevel() >= Constants.PLAYER_MAX_LEVEL) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_BEYOND_MAX_LEVEL, "您已经达到当前最大等级！"));
			return;
		}
		int uplevelExp = LevelExpMG.instance.getLevelForExp(player.getLevel());
		if (player.getExp() >= uplevelExp) {
			StringBuffer info = new StringBuffer();
			int ret = PlayerService.doUpgradeLevel(player, info);
			if (ret == -1) {
				info.append("您已经到达当前最大等级，不能继续升级！");
			}else if (ret == -2) {
				info.append("您的升级经验不够！");
			}
			req.setVO(TAG_INFO, info);
		}else {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_LACK_OF_EXP, "经验不够升级！"));
			return;
		}
		
		response(req, req.getClientResponse());
	}

}
